import axios from "axios";

import nprogress from "nprogress";
//引入样式
import "nprogress/nprogress.css";

// axios.defaults.baseURL = "http://localhost:8081"


const request = axios.create({
    baseURL: "http://localhost:8081",
    timeout:100000,
        headers:{
        'Content-Type':"application/json;charset=utf-8"
    }
})


request.interceptors.request.use(config =>{
    nprogress.start();
    config.headers['Authorization'] = localStorage.getItem("token")
    return config;
})

// 响应拦截器
request.interceptors.response.use(response =>{
    nprogress.done();
    let res = response.data;
    // 业务逻辑错误
    if (res.code === 200 || res.code === 500) {
        return response;
    }
    else {
        Element.Message.error(res.msg?'系统异常':res.msg);
        return Promise.reject(res.msg);
    }
    if (response.status === 401){
        alert("认证失败，请先登录");
        this.router.push("/")
    }
});

export default request
